package com.webcamtracker.configuration;

import com.webcamtracker.image.color.ColorSampleFactory;
import com.webcamtracker.image.color.rgb.RGBColor;

import javax.media.format.VideoFormat;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;


public class RootPref {
    //Root Preferences
    private static Preferences root = new CachedPreferences(Preferences.userRoot().node("WebcamTracker"));
    public static boolean DEF_VALUE_DEBUG = false;
    public static String KEY_DEBUG = "Debug";

    //Window Interator Pref
    private static Preferences windowIteratorPref = getRootPref().node("WindowIterator");
    public static String KEY_DELTA = "Window Iterator delta";
    public static int DEF_VALUE_DELTA = 4;

    //WebCam
    private static Preferences webCamPref = getRootPref().node("WebCam");
    public static String KEY_WEBCAM_ENCODING = "Encoding";
    public static String DEF_VALUE_WEBCAM_ENCODING = VideoFormat.RGB;

    //Color Preferences
    public static Preferences colorPref = getRootPref().node("Color");
    public static String KEY_TRESHOLD = "Treshold";
    public static double DEF_VALUE_TRESHOLD = 2.0;
    public static String KEY_COLOR_TYPE = "ColorType";
    public static String DEF_VALUE_COLOR_TYPE = ColorSampleFactory.getValue(ColorSampleFactory.HSV);
    private static RGBColor searchColor = new RGBColor(160, 16, 17);

    //Grid Prefence
    private static Preferences gridPref = getRootPref().node("Grid");
    public static String KEY_GRID_WIDTH = "Widht";
    public static int DEF_VALUE_GRID_WIDTH = 4;
    public static String KEY_GRID_HEIGHT = "Height";
    public static int DEF_VALUE_GRID_HEIGHT = 4;
    public static String KEY_GRID_ACCEPTANCE = "Acceptance";
    public static int DEF_VALUE_GRID_ACCEPTANCE = 4;

    //Motion Prefences
    private static Preferences motionPref = getRootPref().node("Motion");
    public static String KEY_MOTION_TRESHOLD = "Treshold";
    public static double DEF_VALUE_MOTION_TRESHOLD = 2.0;
    public static String KEY_COLOR_MOTION_TYPE = "ColorType";
    public static String DEF_VALUE_MOTION_COLOR_TYPE = ColorSampleFactory.getValue(ColorSampleFactory.GRAY_SCALE);

    //Color Blob
    private static Preferences blobPref = getRootPref().node("Color Blob");
    public static final String KEY_BLOB_EXPANSION = "Expansion";
    public static final int DEF_VALUE_BLOB_EXPANSION = 8;

    //History
    private static Preferences historyPref = getRootPref().node("History");
    public static final String KEY_HISTORY_SIZE = "Size";
    public static final int DEF_VALUE_HISTORY_SIZE = 20;
    public static final String KEY_HISTORY_DIFERENCE = "Diference";
    public static final float DEF_VALUE_HIST_DIF = 0.1F;
    //Target
    private static Preferences targetPref = getRootPref().node("Target");
    public static final String KEY_MINIMUM_ZOOM_RATIO = "Minimum zoom ration";
    public static final double DEF_VALUE_MINIMUM_ZOOM_RATIO = 0.4;
    public static final String KEY_MAXIMUM_ZOOM_RATIO = "Maximum zoom ration";
    public static final double DEF_VALUE_MAXIMUM_ZOOM_RATIO = 1.3;
    public static final String KEY_MIN_BRIGHTNESS = "Minimum Brightness";
    public static final double DEF_VALUE_MIN_BRIGHTNESS = 0.4;
    public static final String KEY_MIN_SATURATION = "Minimum saturation";
    public static final double DEF_VALUE_MIN_SATURATION = 0.2;
    public static final String KEY_MAX_TOTAL_PERCENTENGE = "Maximimum total percentage";
    public static final double DEF_VALUE_MAX_TOTAL_PERCENTENGE = 0.90;
    public static final String KEY_MINIMUM_PERCENTAGE = "Minimum Percentage";
    public static final double DEF_VALUE_MINIMUM_PERCENTAGE = 0.04;

    public static Preferences getRootPref() {
        return root;
    }


    public static Preferences getGridPref() {
        return gridPref;
    }

    public static void save() {
        try {
            String fileName = "WebcamTracker.pref";
            FileOutputStream fos = new FileOutputStream(new File(fileName));
            root.exportSubtree(fos);
            final String s = new File(fileName).getAbsolutePath();
            System.out.println("s = " + s);

            fos.close();
        } catch (IOException ioe) {
            System.out.println("IOException in exportToFile\n" + ioe);
            ioe.printStackTrace();
        } catch (BackingStoreException e) {
            e.printStackTrace();
        }
    }

    public static RGBColor getSearchColor() {
        return searchColor;
    }


    public static Preferences getMotionPref() {
        return motionPref;
    }

    public static Preferences getWindowIteratorPref() {
        return windowIteratorPref;
    }


    public static Preferences getBlobPref() {
        return blobPref;
    }

    public static Preferences getColorPref() {
        return colorPref;
    }


    public static Preferences getWebCamPref() {
        return webCamPref;
    }


    public static Preferences getHistoryPref() {
        return historyPref;
    }


    public static Preferences getTargetPref() {
        return targetPref;
    }
}
